package com.study.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.study.app.domain.AppUserInvitationRecord;
import com.study.app.domain.bo.AppUserInvitationRecordBo;
import com.study.app.domain.vo.AppUserInvitationRecordVo;
import com.study.app.mapper.AppUserInvitationRecordMapper;
import com.study.app.service.IAppUserInvitationRecordService;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户邀请记录Service业务层处理
 *
 * @author study
 * @date 2023-04-21
 */
@RequiredArgsConstructor
@Service
public class AppUserInvitationRecordServiceImpl implements IAppUserInvitationRecordService {

    private final AppUserInvitationRecordMapper baseMapper;

    /**
     * 查询用户邀请记录
     */
    @Override
    public AppUserInvitationRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户邀请记录列表
     */
    @Override
    public TableDataInfo<AppUserInvitationRecordVo> queryPageList(AppUserInvitationRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUserInvitationRecord> lqw = buildQueryWrapper(bo);
        Page<AppUserInvitationRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户邀请记录列表
     */
    @Override
    public List<AppUserInvitationRecordVo> queryList(AppUserInvitationRecordBo bo) {
        LambdaQueryWrapper<AppUserInvitationRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUserInvitationRecord> buildQueryWrapper(AppUserInvitationRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUserInvitationRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, AppUserInvitationRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getInvitationUserId() != null, AppUserInvitationRecord::getInvitationUserId, bo.getInvitationUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getInvitationPhone()), AppUserInvitationRecord::getInvitationPhone, bo.getInvitationPhone());
        return lqw;
    }

    /**
     * 新增用户邀请记录
     */
    @Override
    public Boolean insertByBo(AppUserInvitationRecordBo bo) {
        AppUserInvitationRecord add = BeanUtil.toBean(bo, AppUserInvitationRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户邀请记录
     */
    @Override
    public Boolean updateByBo(AppUserInvitationRecordBo bo) {
        AppUserInvitationRecord update = BeanUtil.toBean(bo, AppUserInvitationRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUserInvitationRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户邀请记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
